﻿using System;
using System.Diagnostics.Tracing;
using System.Threading;

namespace Sustainalytics.Utils
{
    [EventSource(Name = "Sustainalytics-Universes")]
    public sealed class UniversesEventSource : System.Diagnostics.Tracing.EventSource
    {
        private static Lazy<UniversesEventSource> _lazyLog = new Lazy<UniversesEventSource>(InitLog, LazyThreadSafetyMode.ExecutionAndPublication);

        public static UniversesEventSource Log => _lazyLog.Value;

        [NonEvent]
        private static UniversesEventSource InitLog()
        {
            var log = new UniversesEventSource();
            log.Ping();

            return log;
        }

        [Event(1, Level = EventLevel.LogAlways)]
        public void Ping()
        {
            if (IsEnabled())
            {
                WriteEvent(1);
            }
        }

        [NonEvent]
        public void Exception(Exception ex)
        {
            if (ex != null)
                this.Exception(ex.GetType().ToString(), ex.Message, ex.ToString());
        }

        [Event(2, Level = EventLevel.Error)]
        public void Exception(string exceptionType, string exceptionMessage, string exceptionInformation)
        {
            if (IsEnabled())
                WriteEvent(2, exceptionType, exceptionMessage, exceptionInformation);
        }

        [Event(3, Level = EventLevel.Informational)]
        public void UploadUniversesUpload(DateTime uploadDate, Guid fileId, string fileName, string contentType)
        {
            if (IsEnabled())
                WriteEvent(3, uploadDate, fileId.ToString(), fileName, contentType);
        }

        [Event(4, Level = EventLevel.Informational)]
        public void AddUploadLogs(DateTime createdDate, Guid fileId)
        {
            if (IsEnabled())
                WriteEvent(4, createdDate, fileId.ToString());
        }
    }
}
